package com.lzq.rabbitmqdemo.java_api.topic;

import com.lzq.rabbitmqdemo.java_api.constant.ConnectionConfig;
import com.lzq.rabbitmqdemo.java_api.constant.Constants;
import com.rabbitmq.client.*;

import java.io.IOException;

public class TopicRabbitmqConsumer2 {
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = ConnectionConfig.getFactory();
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(Constants.TOPIC_QUEUE_NAME2, true, false, false, null);
            DefaultConsumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("接收到消息: "+new String(body));
                }
            };
            channel.basicConsume(Constants.TOPIC_QUEUE_NAME2,true,consumer);
            Thread.sleep(10000);
        }
    }
}
